/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UserInterface.PieChart;
import Business.Record.Record;
import Business.RecordsOperation.RecordsOperation;
import java.awt.Font;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Iterator;
import java.util.List;



import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;

public class PieChart {
	ChartPanel frame1;
	public PieChart(String type){
	      DefaultPieDataset data = getDataSet(type);
	      JFreeChart chart = ChartFactory.createPieChart3D(type + "   analysis"
		      ,data,true,false,false);
	      PiePlot pieplot = (PiePlot) chart.getPlot();
	      DecimalFormat df = new DecimalFormat("0.00%");
	      NumberFormat nf = NumberFormat.getNumberInstance();
	      StandardPieSectionLabelGenerator sp1 = new StandardPieSectionLabelGenerator("{0}  {2}", nf, df);
	      pieplot.setLabelGenerator(sp1);
	  
	      pieplot.setNoDataMessage("no data to show");
	      pieplot.setCircular(false);
	      pieplot.setLabelGap(0.02D);
	  
	      pieplot.setIgnoreNullValues(true);
	      pieplot.setIgnoreZeroValues(true);
	      frame1=new ChartPanel (chart,true);
	      chart.getTitle().setFont(new Font("Monospaced 15",Font.BOLD,20));
	      PiePlot piePlot= (PiePlot) chart.getPlot();
	      piePlot.setLabelFont(new Font("Monospaced 15",Font.BOLD,10));
	      chart.getLegend().setItemFont(new Font("Monospaced 15",Font.BOLD,10));
	}
	
    private static DefaultPieDataset getDataSet(String type) {
        DefaultPieDataset dataset = new DefaultPieDataset();
		List<Record> list = new RecordsOperation().getAllRecords();
		if("event".equals(type)){
			int deviceNum = 0;
			int clinicalNum = 0;
			int humanNum = 0;
			Iterator<Record> ite = list.iterator();
			while(ite.hasNext()){
				Record r = ite.next();
				if("Device".equals(r.eventType)){
					deviceNum++;
				}else if("Clinical procedure".equals(r.eventType)){
					clinicalNum++;
				}else if("Human mistake".equals(r.eventType)){
					humanNum++;
				}else{
					System.out.println("event type not matched");
				}
			}
			dataset.setValue("Device", deviceNum);
			dataset.setValue("Clinical procedure", clinicalNum);
			dataset.setValue("Human mistake", humanNum);
		}else if("participant".equals(type)){
			int hospitalNum = 0;
			int clinicsNum = 0;
			int govNum = 0;
			int personalNum = 0;
			Iterator<Record> ite = list.iterator();
			while(ite.hasNext()){
				Record r = ite.next();
				if("Hospital".equals(r.participantType)){
					hospitalNum++;
				}else if("Clinics".equals(r.participantType)){
					clinicsNum++;
				}else if("Government".equals(r.participantType)){
					govNum++;
				}else if("Personal".equals(r.participantType)){
					personalNum++;
				}else{
					System.out.println("participantType not matched");
				}
			}
			dataset.setValue("Hospital", hospitalNum);
			dataset.setValue("Clinics", clinicsNum);
			dataset.setValue("Government", govNum);
			dataset.setValue("Personal", personalNum);
		}else{
			System.out.println("show type not matched");
		}
		return dataset;
	}
	
    public ChartPanel getChartPanel(){
    	return frame1;
    }
}
